package net.diaowen.dwsurvey.service.impl;



import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.diaowen.common.base.service.AccountManager;
import net.diaowen.common.plugs.page.PropertyFilter;
import net.diaowen.common.service.BaseServiceImpl;
import net.diaowen.common.utils.Arithmetic;
import net.diaowen.dwsurvey.dao.LotteryPrizeArithmeticDao;
import net.diaowen.dwsurvey.dao.LotteryPrizeDao;
import net.diaowen.dwsurvey.dao.LotteryPrizeDetailDao;
import net.diaowen.dwsurvey.dao.LotteryPrizeStatusDao;
import net.diaowen.dwsurvey.dao.PrizeTypeDao;
import net.diaowen.dwsurvey.entity.LotteryPrize;
import net.diaowen.dwsurvey.entity.LotteryPrizeArithmetic;
import net.diaowen.dwsurvey.entity.LotteryPrizeDetail;
import net.diaowen.dwsurvey.entity.LotteryPrizeStatus;
import net.diaowen.dwsurvey.entity.Prize;
import net.diaowen.dwsurvey.entity.PrizeType;
import net.diaowen.dwsurvey.service.LotteryPrizeManager;

@Service
public class LotteryPrizeManagerImpl extends BaseServiceImpl<LotteryPrize, String> implements LotteryPrizeManager {
	
	@Autowired
	private AccountManager accountManager;
	
	@Autowired
	private LotteryPrizeDao lotteryPrizeDao;
	
	@Autowired
	private LotteryPrizeDetailDao lotteryPrizeDetailDao;
	
	@Autowired
	private LotteryPrizeArithmeticDao lotteryPrizeArithmeticDao;
	
	@Autowired
	private LotteryPrizeStatusDao lotteryPrizeStatusDao;
	
	@Autowired
	private PrizeTypeDao prizeTypeDao;

	@Override
	public void setBaseDao() {
		this.baseDao=lotteryPrizeDao;
		
	}

	@Override
	public LotteryPrize getByActivityId(String activityId) {
		Criterion criterion=Restrictions.eq("lotteryActivityId", activityId);
		LotteryPrize lp = lotteryPrizeDao.findFirst(criterion);
		if (lp == null) 
			return lp;
		
		lp.setPrizeList(lotteryPrizeDetailDao.find(Restrictions.eq("lotteryPrizeId", lp.getId())));
		return lp;
	}

	@Override
	public List<PrizeType> getPrizeTypeList() {
		List<PropertyFilter> criterions = new ArrayList<PropertyFilter>();
		return prizeTypeDao.find(criterions);
	}

	@Override
	public void savePrizeSetting(LotteryPrize lp) {
		List<PrizeType> ptList = getPrizeTypeList();
		HashMap<String,String> ptMap = new HashMap<String,String>();
		for (PrizeType pt : ptList) {
			ptMap.put(pt.getId(), pt.getTypeName());
		}
		lp.setCreateTime(new Date());
		lp.setCreateUser(accountManager.getCurUser().getId());
		lp.setUpdateTime(new Date());
		lp.setUpdateUser(accountManager.getCurUser().getId());
		lotteryPrizeDao.save(lp);
		List<LotteryPrizeDetail> prizeDetailList = lp.getPrizeList();
		
		List<Prize> prizes = new ArrayList<Prize>();
	    BigDecimal total = new BigDecimal(lp.getParticipants() * lp.getPartTimes());
		for (int i = 0 ;i < 8; i++) {
			prizeDetailList.get(i).setLotteryPrizeId(lp.getId());
			prizeDetailList.get(i).setPrizeNo(i + 1);
			prizeDetailList.get(i).setPrizeTypeName(ptMap.get(prizeDetailList.get(i).getPrizeTypeId()));
			lotteryPrizeDetailDao.save(prizeDetailList.get(i));
			
			
			
            Prize prize = new Prize();
            prize.setPrizeId(i+1);
            BigDecimal qti = new BigDecimal(prizeDetailList.get(i).getQti());
            prize.setProbability(qti.divide(total,2,BigDecimal.ROUND_HALF_UP));
            prize.setQuantity(prizeDetailList.get(i).getQti());
            prizes.add(prize);

		}
		//算法生成中奖内容
	    Arithmetic arithmetic = new Arithmetic();
	    arithmetic.setPrizes(prizes);
	    Integer[] prizeList = arithmetic.getPrizeList(lp.getParticipants() * lp.getPartTimes(),prizes);
	    for (int j = 0; j<prizeList.length; j++) {
	    	LotteryPrizeArithmetic lpa = new LotteryPrizeArithmetic();
	    	lpa.setAllocationNo(j + 1);
	    	lpa.setAllocationPrizeNo(prizeList[j]);
	    	lpa.setLotteryPrizeId(lp.getId());
	    	lotteryPrizeArithmeticDao.save(lpa);
	    }
	    //创建抽奖状态表
	    LotteryPrizeStatus lps = new LotteryPrizeStatus();
	    lps.setCurPrizeNo(1);
	    lps.setTalPrizeNo(lp.getParticipants() * lp.getPartTimes());
	    lps.setLotteryPrizeId(lp.getId());
	    lotteryPrizeStatusDao.save(lps);
	}

	@Override
	public void resetPrizeSetting(String activityId) {
		Map<String,String> map = new HashMap<String,String>();
		LotteryPrize lp = getByActivityId(activityId);
		String d_lps_HQL = "delete from LotteryPrizeStatus lps where lps.lotteryPrizeId = '" + lp.getId() + "'";
		lotteryPrizeStatusDao.batchExecute(d_lps_HQL, map);
		String d_lpa_HQL = "delete from LotteryPrizeArithmetic lpa where lpa.lotteryPrizeId = '" + lp.getId()+ "'";
		lotteryPrizeArithmeticDao.batchExecute(d_lpa_HQL, map);
		String d_lpd_HQL = "delete from LotteryPrizeDetail lpd where lpd.lotteryPrizeId = '" + lp.getId() + "'";
		lotteryPrizeDetailDao.batchExecute(d_lpd_HQL, map);
		lotteryPrizeDao.delete(lp.getId());
	}


}
